<?php

/*
 * Copyright © 2010 - 2013 Modo Labs Inc. All rights reserved.
 *
 * The license governing the contents of this file is located in the LICENSE
 * file located at the root directory of this distribution. If the LICENSE file
 * is missing, please contact sales@modolabs.com.
 *
 */

includePackage('DataModel');
class WeatherWebModule extends WebModule {
    protected $id = 'weather';
    protected static $defaultModel = 'WeatherDataModel';

    public static function getShowTypes() {
        return array(
            'sublife-1' => '今日',
            'sublife-2' => '今晚',
            'sublife-3' => '明日',
        );
    }

    public static function getAreaDetail() {
        return array(
            'Taipei_City,臺北市,N' => '臺北市',
            'New_Taipei_City,新北市,N' => '新北市',
            'Taichung_City,臺中市,C' => '臺中市',
            'Tainan_City,臺南市,S' => '臺南市',
            'Kaohsiung_City,高雄市,S' => '高雄市',
            'Keelung_City,基隆市,N' => '基隆市',
            'Taoyuan_County,桃園縣,N' => '桃園縣',
            'Hsinchu_County,新竹縣,N' => '新竹縣',
            'Hsinchu_City,新竹市,N' => '新竹市',
            'Miaoli_County,苗栗縣,N' => '苗栗縣',
            'Changhua_County,彰化縣,C' => '彰化縣',
            'Nantou_County,南投縣,C' => '南投縣',
            'Yunlin_County,雲林縣,C' => '雲林縣',
            'Chiayi_County,嘉義縣,C' => '嘉義縣',
            'Chiayi_City,嘉義市,C' => '嘉義市',
            'Pingtung_County,屏東縣,S' => '屏東縣',
            'Yilan_County,宜蘭縣,E' => '宜蘭縣',
            'Hualien_County,花蓮縣,E' => '花蓮縣',
            'Taitung_County,臺東縣,E' => '臺東縣',
            'Penghu_County,澎湖縣,I' => '澎湖縣',
            'Kinmen_County,金門縣,I' => '金門縣',
            'Lienchiang_County,連江縣,I' => '連江縣',
        );
    }

    public function getHomeWeather() {
        $modelClass = isset($feedData['MODEL_CLASS']) ? $feedData['MODEL_CLASS'] : self::$defaultModel;
        $feedData['DETAIL_CITY'] = $this->getOptionalModuleVar('DETAIL_CITY', 'New_Taipei_City,新北市,N');
        $arr = explode(",", $feedData['DETAIL_CITY']);

        $content[] = $arr[1];

        $feedData['PARSER_CLASS'] = 'DEGREEDataParser';
        $feedData['BASE_URL'] = $this->getOptionalModuleVar('INDEX_DEGREE', 'http://www.cwb.gov.tw/V7/observe/24real/Data/46688.htm');
        $controller1 = WeatherDataModel::factory($modelClass, $feedData);
        $content[] = $controller1->items();

        $feedData['PARSER_CLASS'] = 'WEATHERDataParser';
        $feedData['BASE_URL'] = 'http://www.cwb.gov.tw/V7/forecast/taiwan/'.$arr[0].'.htm';
        $controller2 = WeatherDataModel::factory($modelClass, $feedData);
        $content[] = $controller2->items();

        return $content;
    }
  
    protected function initializeForPage() {
        switch ($this->page) {

            case 'index':
                $feedData['DETAIL_CITY'] = $this->getOptionalModuleVar('DETAIL_CITY', 'New_Taipei_City,新北市,N');
                $arr = explode(",", $feedData['DETAIL_CITY']);
                $feedData['BASE_URL'] = "http://www.cwb.gov.tw/V7/forecast/taiwan/inc/city/".$arr[0].".htm";
                $modelClass = isset($feedData['MODEL_CLASS']) ? $feedData['MODEL_CLASS'] : self::$defaultModel;
                if (!isset($feedData['PARSER_CLASS'])) {
                    $feedData['PARSER_CLASS'] = 'WEEKLYDataParser';
                }

                $controller = WeatherDataModel::factory($modelClass, $feedData);
                $this->assign('list', $controller->items());
                $this->assign('city', $arr[1]);
                break;

            case 'pane':
                $feedData['HTML_ID'] = $this->getOptionalModuleVar('HTML_ID', 'sublife-1');
                $feedData['DETAIL_CITY'] = $this->getOptionalModuleVar('DETAIL_CITY', 'New_Taipei_City,新北市,N');
                $arr = explode(",", $feedData['DETAIL_CITY']);
                $modelClass = isset($feedData['MODEL_CLASS']) ? $feedData['MODEL_CLASS'] : self::$defaultModel;
                if (!isset($feedData['PARSER_CLASS'])) {
                    $feedData['PARSER_CLASS'] = 'TODAYDataParser';
                }
                $feedData['BASE_URL'] = 'http://www.cwb.gov.tw/V7/life/Life_'.$arr[2].'.htm';
                $controller1 = WeatherDataModel::factory($modelClass, $feedData);
                $content[] = $controller1->items();

                $feedData['PARSER_CLASS'] = 'DEGREEDataParser';
                $feedData['BASE_URL'] = $this->getOptionalModuleVar('INDEX_DEGREE', 'http://www.cwb.gov.tw/V7/observe/24real/Data/46688.htm');
                $controller2 = WeatherDataModel::factory($modelClass, $feedData);
                $content[] = $controller2->items();

                $feedData['PARSER_CLASS'] = 'WEATHERDataParser';
                $feedData['BASE_URL'] = 'http://www.cwb.gov.tw/V7/forecast/taiwan/'.$arr[0].'.htm';
                $controller3 = WeatherDataModel::factory($modelClass, $feedData);
                $content[] = $controller3->items();

                $this->assign('list1', $content[0]);
                $this->assign('list2', $content[1]);
                $this->assign('list3', $content[2]);
                $this->assign('city', $arr[1]);
                break;
        }
    }
}
